<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Capital;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;

class CapitalController extends Controller
{
    public function save(Request $request)
    {
        $id = $request->input("id","");
        $name = $request->input("name","");
        $phone = $request->input("phone","");
        $capitalMainName = $request->input("capitalMainName","");
        $moneyRate = $request->input("moneyRate","");
        $password = $request->input("password","");
        $remark = $request->input("remark","");
        empty($remark) and $remark = "";
        $beginDate = $request->input("beginDate","");
        empty($beginDate) and $beginDate = null;

        $user = Capital::query()
            ->when($id, function (Builder $query)use($id) {
                $query->where("id","!=",$id);
            })
            ->where("phone",$phone)
            ->exists();
        if($user) return $this->httpError("资方登录手机号已存在");

        $model = Capital::query()->find($id);
        if(empty($model)){
            $model = new Capital;
            $model->capital_no = "ZF".date("YmdHis").rand(1000,9999);
        }
        $model->name = $name;
        $model->phone = $phone;
        $model->capital_main_name = $capitalMainName;
        $model->money_rate = $moneyRate;
        $model->password = Crypt::encrypt($password);
        $model->remark = $remark;
        $model->begin_date = $beginDate;
        $model->save();
        return $this->httpSuccess();
    }

    public function pageItems(Request $request){
        $capitalNo = $request->input("capitalNo","");
        $name = $request->input("name","");
        $phone = $request->input("phone","");
        $capitalMainName = $request->input("capitalMainName","");
        $createdAtLeft = $request->input("createdAtLeft","");
        $createdAtRight = $request->input("createdAtRight","");
        $page = $request->input("page",1);
        $pageSize = $request->input("pageSize",20);
        $paginate = Capital::query()
            ->with([
                "capitalRel"
            ])
            ->when($capitalNo, function (Builder $query)use($capitalNo) {
                $query->where("capital_no",$capitalNo);
            })
            ->when($phone, function (Builder $query)use($phone) {
                $query->where("phone",$phone);
            })
            ->when($name, function (Builder $query)use($name) {
                $query->where("name",$name);
            })
            ->when($capitalMainName, function (Builder $query)use($capitalMainName) {
                $query->where("capital_main_name",$capitalMainName);
            })
            ->when($createdAtLeft, function (Builder $query)use($createdAtLeft) {
                $query->where("created_at",">=",$createdAtLeft);
            })
            ->when($createdAtRight, function (Builder $query)use($createdAtRight) {
                $query->where("created_at","<=",$createdAtRight);
            })
            ->where("is_show",1)
            ->select("*")
            ->orderByDesc("id")
            ->paginate($pageSize,['*'],'page',$page);
        $items = $paginate->items();
        foreach ($items as $item){
            $item->password = Crypt::decrypt($item->password);
            $item->all_money = $item->capitalRel->sum("money");
            $item->all_num = $item->capitalRel->count();
            unset($item->capitalRel);
        }
        return $this->httpSuccess([
            "total"=>$paginate->total(),
            "items"=>$paginate->items(),
        ]);

    }

    public function delete(Request $request){
        $ids = $request->input("ids",[]);
        Capital::query()->whereIn("id",$ids)->delete();
        return $this->httpSuccess();
    }
}













